// -------------------------------------------------------------
// api: respool.area.server.create
// time: 2020-07-31
// author: minner
// -------------------------------------------------------------
exports['respool.area.server.create'] = obj_api = {
    '1.0': function (args, session, callback) {
        if (!session.account_id) {
            return callback(12, null, '会话已失效');
        }

        // if (!args.respool_id || typeof args.respool_id != 'number') {
        //     return callback(11, null, '缺少参数(respool_id)');
        // }
        if (!args.respool_area_id || typeof args.respool_area_id != 'number') {
            return callback(11, null, '缺少参数(respool_area_id)');
        }
        // if (!args.system_class_id || typeof args.system_class_id != 'number') {
        //     return callback(11, null, '缺少参数(system_class_id)');
        // }
        if (!args.system_id || typeof args.system_id != 'number') {
            return callback(11, null, '缺少参数(system_id)');
        }
        if (!args.server_name || typeof args.server_name != 'string') {
            return callback(11, null, '缺少参数(server_name)');
        }
        if (!args.server_type || typeof args.server_type != 'number') {
            return callback(11, null, '缺少参数(server_type)');
        }
        if (!args.deploy_type || typeof args.deploy_type != 'number') {
            return callback(11, null, '缺少参数(deploy_type)');
        }
        if (!args.cpu || typeof args.cpu != 'number') {
            return callback(11, null, '缺少参数(cpu)');
        }
        if (!args.memory || typeof args.memory != 'number') {
            return callback(11, null, '缺少参数(memory)');
        }
        if (!args.harddisks || typeof args.harddisks != 'string') {
            return callback(11, null, '缺少参数(harddisks)');
        }
        if (!args.description || typeof args.description != 'string') {
            return callback(11, null, '缺少参数(description)');
        }


        uim.db_query3({
            reqs: [
                {
                    sql: "SELECT respool_id FROM deploy_respool_area WHERE respool_area_id = " + args.respool_area_id + " LIMIT 1",
                    done: function ({ err, results, fields, start, end }) {
                        args.respool_id = results[0].respool_id;
                    }
                },
                {
                    sql: "SELECT system_class_id FROM deploy_respool_system WHERE respool_system_id = " + args.system_id + " LIMIT 1",
                    done: function ({ err, results, fields, start, end }) {
                        args.system_class_id = results[0].system_class_id;
                    }
                },
            ],
            callback: function (err, successCount, errorCount, totalTime) {
                if (err) {
                    return callback(1, null, '查询数据库出错' + err)
                }

                let sql_insert = "INSERT INTO `deploy_respool_area_server`" +
                    " (`system_class_id`, `system_id`, `server_name`, `server_type`, `deploy_type`, `cpu`, `memory`, `harddisks`, `create_time`, `creator`, `status`, `description`, `respool_area_id`, `respool_id`)" +
                    " VALUES (" +
                    mysql.escape(args.system_class_id) + ", " +
                    mysql.escape(args.system_id) + ", " +
                    mysql.escape(args.server_name) + ", " +
                    args.server_type + ", " +
                    args.deploy_type + ", " +
                    args.cpu + ", " +
                    args.memory + ", " +
                    mysql.escape(args.harddisks) + ", '" +
                    uim.dateTime() + "', " +
                    session.account_id + ", '1', " +
                    mysql.escape(args.description) + ", " +
                    args.respool_area_id + ", " +
                    args.respool_id + ")";

                uim.db_query(sql_insert, function (err, results, fields) {
                    if (err) {
                        return callback(6, null, '数据库查询出错：' + err);
                    }
                    callback(0, {
                        respool_area_server_id: results.insertId,
                    });
                });
            }
        });
    },

};
